/**
 * https://www.jianshu.com/p/a9a55c03f768
 */
const puppeteer = require('puppeteer');

/**
 * 获取segmentfault
 */
const getSegmentfault = async () => {
  const browser = await (puppeteer.launch({
    timeout: 15000,
    headless: true
  }));
  const page = await browser.newPage();

  await page.goto('https://segmentfault.com/blogs/hottest');
  await page.waitFor(2000);

  const segResult = await page.evaluate(() => {
    const $ = window.$;
    const segView = $('.stream-list');
    const result = [];
    if (segView.length) {
      const itemArr = segView.find('.stream-list__item');
      itemArr.each((index, item) => {
        result.push({
          title: $($(item).find('.title')[0]).text(),
          picURL: 'https://cdn.segmentfault.com/v-5c498373/global/img/logo-b.svg',
          messageURL: `https://segmentfault.com${$(item).find('.title a').attr('href')}`
        });
      });
    }
    return result;
  });

  // console.log(segResult);
  browser.close();
  return segResult;
};

/**
 * 获取juejin
 */
const getJueJin = async () => {
  const browser = await (puppeteer.launch({
    timeout: 15000,
    headless: true
  }));
  const page = await browser.newPage();

  await page.goto('https://juejin.im/welcome/frontend');
  await page.waitFor(2000);

  const juejinResult = await page.evaluate(() => {
    const view = document.querySelectorAll('.entry-list');
    const result = [];
    if (view.length) {
      const itemArr = view[0].querySelectorAll('li.item .entry-box');
      for (let i = 0; i < itemArr.length; i++) {
        result.push({
          title: itemArr[i].querySelector('.title-row .title').innerText,
          picURL: 'https://b-gold-cdn.xitu.io/v3/static/img/logo.a7995ad.svg',
          messageURL: itemArr[i].querySelector('.title-row .title').href
        });
      }
    }
    return result;
  });

  // console.log(juejinResult);
  browser.close();
  return juejinResult;
};

const spider = async () => {
  return {
    segmentfault: await getSegmentfault(),
    juejin: await getJueJin()
  } 
}

module.exports = spider;